
angular.module("starter",["ionic"]).run(function ($ionicPlatform,$rootScope) {

 //所有控制器都可以访问$rootscope的内容
 $rootScope.list=[{id:0,title:"qqqq0"},{id:1,title:"qqqq1"},{id:2,title:"qqqq2"}];

})
.config(function ($stateProvider,$urlRouterProvider,$ionicConfigProvider) {
  /**
   * 路由状态的名字
   * 路由的详细配置
   * ui-sref="l路由状态名字 state 的字符串"
   */
  $ionicConfigProvider.platform.android.tabs.position("bottom");
  $ionicConfigProvider.platform.android.navBar.alignTitle("center");

  $stateProvider.state("tab",{
    url:"/tab",
    templateUrl:"templates/mytable.html",
    controller:"",
    abstract:true,

  }).state("login",{
    url:"/login",
    templateUrl:"templates/login.html",
    controller:"logincontroller"


  })
    .state("register",{
      url:"/register",
      templateUrl:"templates/register.html",
      controller:"regController"


    })
    .state("findpassword",{
      url:"/findpassword",
      templateUrl:"templates/findpassword.html",
      controller:"findcontroller"


    })
    .state("tab.home",{
      url:"/home",

      views: {
        'tab-home': {
          templateUrl: 'templates/home.html',
          controller: 'homecontroller'
        }
      }
    })
    .state("tab.message",{
      url:"/home/message",

      views: {
        'tab-home': {
          templateUrl: 'templates/message.html',
          controller: 'messagecontroller'
        }
      }
    })
    .state("tab.space",{
      url:"/space",

      views: {
        'tab-space': {
          templateUrl: 'templates/space.html',
          controller: 'spacecontroller'
        }
      }
    })
      .state("tab.friend",{
        url:"/friend",

        views: {
          'tab-friend': {
            templateUrl: 'templates/friend.html',
            controller: ''
          }
        }
      })
    .state("tab.setting",{
      url:"/setting",
      views: {
        'tab-setting': {
          templateUrl: 'templates/setting.html',
          controller: ''
        }
      }
    })

  .state("tab.pay",{
    url:"/setting/pay",

    views: {
      'tab-setting': {
        templateUrl: 'templates/pay.html',
        controller: ''
      }
    }
  })
    .state("tab.youhui",{
      url:"/setting/pay/youhui",
      views: {
        'tab-setting': {
          templateUrl: 'templates/youhui.html',
          controller: ''
        }
      }
    })
  if(localStorage.getItem("user"))
  {
    $urlRouterProvider.otherwise("/tab/home")
  }
  else {
    $urlRouterProvider.otherwise("/login")

  }

  //
})
  .controller("logincontroller",function ($scope,$state,$rootScope,$http,$ionicLoading,$ionicPopup,$ionicActionSheet,$httpParamSerializerJQLike) {
    $scope.info={};
  $scope.login=function () {
    $http({
      method:"POST",
      url:"http://localhost:3000/users/login",
      data:$httpParamSerializerJQLike({
        username:$scope.info.username,
        password:$scope.info.password,
      }),
      headers:{
        "content-type":"application/x-www-form-urlencoded"
      }

    }).then(function (succ) {
      console.log(succ);
      $rootScope.user=succ.data.data;
      console.log($rootScope.user);
      if($rootScope.user!=="undefined"){
        localStorage.setItem("user",$rootScope.user.username);
        localStorage.setItem("userid",$rootScope.user.id);
      $state.go("tab.home");
      }

    })
  }

})
  .controller("regController",function ($scope,$state,$rootScope,$http,$ionicLoading,$ionicPopup,$ionicActionSheet,$httpParamSerializerJQLike) {

  $scope.info = {};

  $scope.gologin=function () {
    $state.go("login")
  }
  $scope.toRegister = function () {

    console.log($scope.info.username);
    $http({
      method:"POST",
      url:"http://localhost:3000/users/register",
      data:$httpParamSerializerJQLike({
        username:$scope.info.username,
        password:$scope.info.password,
        phone:$scope.info.phone,
      }),
      headers:{
        "content-type":"application/x-www-form-urlencoded"
      }

    }).then(function (succ) {
      console.log(succ);

    })
  }
  $scope.register=function () {

  }

})
  .controller("findcontroller",function ($scope,$state,$rootScope,$http,$ionicLoading,$ionicPopup,$ionicActionSheet,$httpParamSerializerJQLike) {
    $scope.info={};
  $scope.gologin=function () {
    $state.go("login")
  }
  $scope.findpassword=function () {
    console.log($scope.info.username);
    $http({
      method:"POST",
      url:"http://localhost:3000/users/findpassword",
      data:$httpParamSerializerJQLike({
        username:$scope.info.username,
        phone:$scope.info.phone,
      }),
      headers:{
        "content-type":"application/x-www-form-urlencoded"
      }

    }).then(function (succ) {
      console.log(succ);
    })
  }

})
  .controller("messagecontroller",function ($scope,$state,$rootScope,$http,$ionicLoading,$ionicPopup,$ionicActionSheet,$httpParamSerializerJQLike) {
    $scope.info={};
    $scope.gologin=function () {
      $state.go("login")
    }
    var map= new BAIDUBMAP();
      map.Geolocation().then(function (result) {
        console.log(result.point);
        $scope.result=result;
        $scope.postmessage=function () {
          console.log(localStorage.getItem("userid"),$scope.info.title);

          $http({
            method:"POST",
            url:"http://localhost:3000/messages/postmessage",
            data:$httpParamSerializerJQLike({
              username:localStorage.getItem("user"),
              userid:localStorage.getItem("userid"),
              title:$scope.info.title,
              article:$scope.info.article,
              lng:$scope.result.point.lng,
              lat:$scope.result.point.lat
            }),
            headers:{
              "content-type":"application/x-www-form-urlencoded"
            }

          }).then(function (succ) {
            console.log(succ);
          })
        }
      });


    // function geolocation() {
    //   $scope.jW=map.Geolocation();
    // }
    // geolocation();



  })
  .controller("spacecontroller",function ($scope,$state,$rootScope,$http,$ionicLoading,$ionicPopup,$ionicActionSheet,$httpParamSerializerJQLike) {
    $scope.info={};
  function height() {
    var height = document.querySelector("#map");
    console.log(innerHeight);
    height.style.height = (innerHeight - 88) + "px";
  }
   new height();

    var map= new BAIDUBMAP();


  function allmessage() {
    map.init();
    map.CopyrightControl();
    map.GeolocationControl();
    map.Geolocation();
    $http({
      method:"get",
      url:"http://localhost:3000/messages/allmessage",
      // data:$httpParamSerializerJQLike({
      //   username:localStorage.getItem("user"),
      // }),
      // headers:{
      //   "content-type":"application/x-www-form-urlencoded"
      // }

    }).then(function (succ) {
      console.log(succ);
      $scope.data=succ.data.data;
      $scope.data.forEach(function (t) {
        map.infoWindow(t.lng,t.lat,t.title,t.article+t.time);
        console.log(t.article+t.time)
      })
    })
  }
  allmessage();

  })
  .controller("homecontroller",function ($scope,$state,$rootScope,$http,$ionicLoading,$ionicPopup,$ionicActionSheet,$httpParamSerializerJQLike){
   function homeload() {
     $http({
       method:"POST",
       url:"http://localhost:3000/messages/individualmessage",
       data:$httpParamSerializerJQLike({
         username:localStorage.getItem("user"),
       }),
       headers:{
         "content-type":"application/x-www-form-urlencoded"
       }

     }).then(function (succ) {
       console.log(succ);
       $scope.data=succ.data.data;
     })
   }
      homeload();
    $scope.showAlert = function(x) {
      var alertPopup = $ionicPopup.alert({
        title: x.title,
        template: x.article+"<br/>"+x.time
      })};

  })
